package com.tentative.competition.schedule.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailUtil;
import com.tentative.competition.common.core.domain.entity.SysUser;
import com.tentative.competition.common.utils.SecurityUtils;
import com.tentative.competition.schedule.service.CodeService;
import com.tentative.competition.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


/**
 * @auther: Smile Huang
 * @date: 2023/6/22 19:06
 * @desc:
 * @version: 2.0
 */
@Service
public class CodeServiceImpl implements CodeService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ISysUserService userService;



    /**
     * 获取邮箱验证码
     * @param email
     * @return
     */
    @Override
    public void getCode(String email,String code) {
        //邮箱验证码的key
        String codeKey = email + code;
        //查询当前邮箱验证码在redis是否存在
        if (stringRedisTemplate.hasKey(codeKey)){
            throw new RuntimeException("验证码已发送，请关注邮箱或垃圾邮件中！");
        }
        //生成验证码
        String emailCode = emailCode();
        //准备发送邮件
        MailUtil.send(email, "验证码", "【湖北省微生物培养皿艺术设计大赛】您的验证码为：【"+emailCode+"】，5分钟内有效，请不要透露给别人，以免个人信息泄露。请认准hbwsw@mail.hzau.edu.cn是大赛唯一官方邮箱，谨防被骗。", false);
        //储存验证码到redis
        stringRedisTemplate.opsForValue().set(codeKey, emailCode,300, TimeUnit.SECONDS);
    }

    @Override
    public void getForgetPasswordCode(String email,String code){
        if (StrUtil.isBlank(email)){
            throw new RuntimeException("邮箱不能为空！");
        }
        //验证邮箱是否存在
        SysUser sysUser = userService.selectUserByEmail(email);
        if (ObjUtil.isEmpty(sysUser)){
            throw new RuntimeException("当前邮箱在系统中不存在，请检查之后再进行此操作！");
        }
        //邮箱验证码的key
        String codeKey = email + code;
        //查询当前邮箱验证码在redis是否存在
        if (stringRedisTemplate.hasKey(codeKey)){
            throw new RuntimeException("验证码已发送，请关注邮箱或垃圾邮件中！");
        }
        //生成验证码
        String emailCode = emailCode();
        //准备发送邮件
        MailUtil.send(email, "验证码", "【湖北省微生物培养皿艺术设计大赛】您的验证码为：【"+emailCode+"】，5分钟内有效，请不要透露给别人，以免个人信息泄露。请认准hbwsw@mail.hzau.edu.cn是大赛唯一官方邮箱，谨防被骗。", false);
        //储存验证码到redis
        stringRedisTemplate.opsForValue().set(codeKey, emailCode,300, TimeUnit.SECONDS);
    }

    @Override
    public void getWorksCode(String codeKey) {
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        if (ObjUtil.isEmpty(sysUser)){
            throw new RuntimeException("当前用户不存在！");
        }
        //作品验证码的key
        String key = sysUser.getEmail() + codeKey+sysUser.getUserId();
        //查询当前邮箱验证码在redis是否存在
        if (stringRedisTemplate.hasKey(key)){
            throw new RuntimeException("验证码已发送，请关注邮箱或垃圾邮件中！");
        }
        //生成验证码
        String emailCode = emailCode();
        //准备发送邮件
        MailUtil.send(sysUser.getEmail(), "验证码", "【湖北省微生物培养皿艺术设计大赛】您的验证码为：【"+emailCode+"】，5分钟内有效，请不要透露给别人，以免个人信息泄露。请认准hbwsw@mail.hzau.edu.cn是大赛唯一官方邮箱，谨防被骗。", false);
        //储存验证码到redis
        stringRedisTemplate.opsForValue().set(key, emailCode,300, TimeUnit.SECONDS);
    }


    private String emailCode(){
        return (int)((Math.random()*9+1)*100000)+"";
    }

}
